import bpy


class MY_MT_Pie(bpy.types.Menu):
    bl_idname = "MY_MT_pietest"
    bl_label = "第一个PIE菜单测试"

    def draw(self, context):
        layout = self.layout
        pie = layout.menu_pie()
        if context.area.type == "VIEW_3D":
            # 左
            p1 = pie.operator("object.select_all", text="反选")
            p1.action = "INVERT"
            # 右
            p2 = pie.operator("object.select_all", text="全选")
            p2.action = "SELECT"
            # 底
            p3 = pie.operator("object.select_all", text="取消选择")
            p3.action = "DESELECT"
            # 顶
            p4 = pie.operator("object.select_all", text="默认")


bpy.utils.register_class(MY_MT_Pie)

keymap = {'idname': 'wm.call_menu_pie',  # 所绑定的操作符
          'oskey': False,  # 组合 win键(win) / command键(mac)
          'ctrl': False,   # 组合 ctrl
          'shift': True,  # 组合 shift
          'alt': False,    # 组合 alt(win) / option(mac)
          'key_modifier': 'NONE',
          'any': False,
          'type': 'P',        # 其他按键
          'value': 'PRESS',  # 定义按键状态
          "properties": {"name": "MY_MT_pietest"}}  # 定义属性

kc = bpy.context.window_manager.keyconfigs.addon
km = kc.keymaps.new(name='Window', space_type='EMPTY')
kmi = km.keymap_items.new(idname=keymap['idname'], type=keymap['type'],
                          value=keymap['value'], ctrl=keymap['ctrl'],
                          shift=keymap['shift'], alt=keymap['alt'],
                          oskey=keymap['oskey'], any=keymap['any'],
                          key_modifier=keymap['key_modifier'])

for pn, p in keymap["properties"].items():
    setattr(kmi.properties, pn, p)

kmi.active = True
# 显示绑定的按键
# rna_keymap_ui.draw_kmi([], kc, km, kmi, layout, 0)
